package com.tigercel.xiangchengyun.ui.activities

import android.annotation.SuppressLint
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v7.app.AppCompatActivity
import android.util.Log
import android.view.View
import com.tigercel.xiangchengyun.R
import com.tigercel.xiangchengyun.ui.custom.verticaltablayout.VerticalTabLayout
import com.tigercel.xiangchengyun.ui.custom.verticaltablayout.widget.ITabView
import com.tigercel.xiangchengyun.ui.custom.verticaltablayout.widget.QTabView
import com.tigercel.xiangchengyun.ui.custom.verticaltablayout.widget.TabView
import com.tigercel.xiangchengyun.ui.fragments.ChannelSubFragment
import com.tigercel.xiangchengyun.utils.ActivityStackManager
import com.tigercel.xiangchengyun.utils.TitleBuilder
import java.lang.ref.WeakReference
import java.util.*

class NewChannelMoreActivity : AppCompatActivity() {

    private var tablayout: VerticalTabLayout? = null
    private var mFragmentManager: FragmentManager? = null
    private var mCurrentFragment: Fragment? = null
    private val fragmentList = HashMap<String, Fragment>()
    private var fragment : ChannelSubFragment? = null
    private var fragment1 : ChannelSubFragment? = null
    private var fragment2 : ChannelSubFragment? = null

    @SuppressLint("ResourceAsColor")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new_channel_more)
        ActivityStackManager.getInstance().addActivity(WeakReference(this))

        TitleBuilder(this)
                .setTitleText("中央厨房.内容共享")
                .setLeftImage(R.mipmap.ic_up_white)
                .setLeftOnClickListener(View.OnClickListener { finish() })
                .setRightText("完成")

        tablayout = findViewById(R.id.tablayout)
        tablayout!!.addTab(QTabView(this).setTitle(ITabView.TabTitle.Builder().setContent("默认").build()))
        tablayout!!.addTab(QTabView(this).setTitle(ITabView.TabTitle.Builder().setContent("推荐").build()))
        tablayout!!.addTab(QTabView(this).setTitle(ITabView.TabTitle.Builder().setContent("本地").build()))
        tablayout!!.addOnTabSelectedListener(object : VerticalTabLayout.OnTabSelectedListener {
            override fun onTabSelected(tab: TabView, position: Int) {
                //                tab.setBackground(R.color.white);
                when (position) {
                    0 -> {
                        switchFragment("fragment", fragment!!)
                    }
                    1 -> {
                        switchFragment("fragment1", fragment1!!)
                    }
                    else -> {
                        switchFragment("fragment2", fragment2!!)
                    }
                }
            }

            override fun onTabReselected(tab: TabView, position: Int) {}
        })
        fragment = ChannelSubFragment.newInstance(0)
        fragment1 = ChannelSubFragment.newInstance(1)
        fragment2 = ChannelSubFragment.newInstance(2)

        mFragmentManager = supportFragmentManager
        mCurrentFragment = createFragment("fragment", fragment!!, true)
        mFragmentManager!!.beginTransaction().add(R.id.fl_content, mCurrentFragment).commit()

    }

    fun createFragment(name: String, fragment: Fragment,isObtain: Boolean): Fragment? {
        var resultFragment: Fragment?
        if (fragmentList.containsKey(name)) {
            resultFragment = fragmentList[name]
        } else {
            resultFragment = fragment
            if (isObtain)
                fragmentList[name] = fragment
        }
        return resultFragment
    }

    //切换Fragment
    private fun switchFragment(name: String, fragment: Fragment) {
        val to = createFragment(name, fragment, true)
        if (to != null) {
            if (to.isAdded) {
                Log.i("====", "Added")
                mFragmentManager!!.beginTransaction().hide(mCurrentFragment).show(to).commitAllowingStateLoss()
            } else {
                Log.i("====", "Not Added")
                mFragmentManager!!.beginTransaction().hide(mCurrentFragment).add(R.id.fl_content, to).commitAllowingStateLoss()
            }
        }
        mCurrentFragment = to
    }

    //    private class MyPagerAdapter extends PagerAdapter implements TabAdapter {
    //        List<String> titles;
    //
    //        public MyPagerAdapter() {
    //            titles = new ArrayList<>();
    //            Collections.addAll(titles, "Android", "IOS", "Web", "JAVA", "C++",
    //                    ".NET", "JavaScript", "Swift", "PHP", "Python", "C#", "Groovy", "SQL", "Ruby");
    //        }
    //
    //        @Override
    //        public int getCount() {
    //            return titles.size();
    //        }
    //
    //        @Override
    //        public TabView.TabIcon getIcon(int position) {
    //            return null;
    //        }
    //
    //        @Override
    //        public TabView.TabTitle getTitle(int position) {
    //
    //            return new TabView.TabTitle.Builder()
    //                    .setContent(titles.get(position))
    //                    .setTextColor(Color.WHITE, 0xBBFFFFFF)
    //                    .build();
    //        }
    //
    //        @Override
    //        public int getBackground(int position) {
    //            return 0;
    //        }
    //
    //        @Override
    //        public boolean isViewFromObject(View view, Object object) {
    //            return view == object;
    //        }
    //
    //        @Override
    //        public Object instantiateItem(ViewGroup container, int position) {
    //            TextView tv = new TextView(NewChannelMoreActivity.this);
    //            tv.setTextColor(Color.WHITE);
    //            tv.setGravity(Gravity.CENTER);
    //            tv.setText(titles.get(position));
    //            tv.setTextSize(18);
    //            container.addView(tv);
    //            return tv;
    //        }
    //
    //        @Override
    //        public void destroyItem(ViewGroup container, int position, Object object) {
    //            container.removeView((View) object);
    //        }
    //    }
    //
    //    protected int dp2px(float dp) {
    //        final float scale = this.getResources().getDisplayMetrics().density;
    //        return (int) (dp * scale + 0.5f);
    //    }


    override fun onDestroy() {
        super.onDestroy()
        ActivityStackManager.getInstance().removeActivity(WeakReference(this))
    }
}
